﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vuonthieunhi.Areas.Administration.Models;
using vuonthieunhi.Library;
using vuonthieunhi.Models;
using System.Web.Security;

namespace TSV.Areas.Administration.Controllers
{
    public class BaseController : Controller
    {
        public AuthenticationProjectPrincipal principal = null;
        protected AuthenticateDao auDao = new AuthenticateDao();
        
        private const string cookieName = "AccessibleMenus";
        private string msgHoder = "<div id=\"systemmessage\" class=\"{0}\">{1}</div>";

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            HttpCookie cookie = requestContext.HttpContext.Request.Cookies[Constants.SERVER_COOKIE];
            if (cookie != null)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                FormsIdentity identity = new FormsIdentity(ticket);
                UserData udata = UserData.CreateUserData(ticket.UserData);
                principal = new AuthenticationProjectPrincipal(identity, udata);
                HttpContext.User = principal;
                return;
            }
            else
            {
                Response.Write("<script>window.location='/Administration/Authenticate/Index'</script>");
                Response.End();
            }
        }

        #region helper methods



        //duy hung sua
        protected void ShowMessage(Message msg)
        {
            string message = string.Empty;
            if (msg.MsgType == MessageType.Error)
            {
                message = string.Format(msgHoder, "msgError", msg.MsgText);
            }
            else if (msg.MsgType == MessageType.Warning)
            {
                message = string.Format(msgHoder, "msgWarning", msg.MsgText);
            }
            else
            {
                message = string.Format(msgHoder, "msgSuccess", msg.MsgText);
            }

            TempData["Message"] = message;
        }

        //duy hung sua
        protected void ShowAlertMessage(Message msg)
        {
            TempData["Message"] = "<script>alert('" + msg.MsgText + "');</script>";
        }

        #endregion

        public class FileUploadJsonResult : JsonResult
        {
            public override void ExecuteResult(ControllerContext context)
            {
                this.ContentType = "text/html";
                context.HttpContext.Response.Write("<textarea>");
                base.ExecuteResult(context);
                context.HttpContext.Response.Write("</textarea>");
            }
        }



    }
}
